package org.eminem.elasticsearch;

import java.net.InetAddress;

import my.elasticsearch.client.Client;
import my.elasticsearch.client.transport.TransportClient;
import my.elasticsearch.common.settings.Settings;
import my.elasticsearch.common.transport.InetSocketTransportAddress;

/**
 * ES Client Class
 */
public class ESClient {

	private static Client client;

	public static synchronized Client getEsClient(String clusterName,String host, int port) throws Exception {
		if(null == client){
			Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).build();
			client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
		}
		return client;
	}
	
	public static void closeClient() {
		if(null != client) {
			client.close();
		}
	}
}
